<?php
/**
 * +--------------------------------------------------------+
 * @Created by Phpstorm.
 * @Category NavController.php
 * @Depiction
 * @Author hwy <429208948@qq.com>
 * @Date 2022/3/26  17:05
 * +--------------------------------------------------------+
 */

namespace App\Http\Controllers\Admin\V1;


use App\Util\Routes;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class NavController extends Controller
{
    public function __construct()
    {

    }

    public function index(Request $request)
    {
        $user = $request->user();

        $routes = new Routes($user);
        $list   = $routes->nav();


        return $this->responseSuccessData('获取成功', $list);
    }

    public function navSetNoCache(Request $request)
    {
        $user = $request->user();

        $data   = (array)$request->post('data');
        $routes = new Routes($user);
        $data   = collect($data)->mapWithKeys(function (array $array): array {
            return [$array['name'] => $array['no_cache']];
        });
        Cache::forget($routes->cacheKey());
        Cache::store('redis')->forever($routes->cacheKey(), $data);


        return $this->responseSuccessData('获取成功', $data);
    }

    public function navSetAffix(Request $request)
    {
        $user = $request->user();

        $data   = (array)$request->post('data');
        $routes = new Routes($user);
        $data   = collect($data)->mapWithKeys(function (array $array): array {
            return [$array['name'] => $array['affix']];
        });
        Cache::forget($routes->affixKey());
        Cache::store('redis')->forever($routes->affixKey(), $data);


        return $this->responseSuccessData('获取成功', $data);
    }

}
